package com._21cn._front.cms.api;

import com._21cn.cms.article.entity.Article;
import com._21cn.cms.article.service.ArticlePictureService;
import com._21cn.cms.article.service.ArticleService;
import com._21cn.cms.article.service.ArticleThumbPictureService;
import com._21cn.cms.common.utils.ResponseUtil;
import com._21cn.framework.flip.PageList;
import com._21cn.framework.http.HttpRequestInfo;
import com._21cn.framework.utils.DateUtil;
import com._21cn.framework.utils.StringUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping({"/api/article"})
public class ArticleApiController
{

    @Autowired
    private ArticleService articleService;

    @Autowired(required=false)
    private ArticleThumbPictureService articleThumbPictureService;

    @Autowired(required=false)
    private ArticlePictureService articlePictureService;

    @RequestMapping({"/getListByColumns"})
    public void getListByColumns(HttpServletRequest request, HttpServletResponse response, ModelMap model)
    {
        try
        {
            HttpRequestInfo reqInfo = new HttpRequestInfo(request);
            Long ltPublishTimeLong = Long.valueOf(reqInfo.getLongParameter("ltPublishTime", -1L));
            int hasPicture = reqInfo.getIntParameter("hasPicture", 1);
            int pageSize = reqInfo.getIntParameter("pageSize", 10);
            String columnIds = reqInfo.getParameter("columnIds", "");
            List columnIdList = StringUtil.str2IntList(columnIds);
            if (columnIdList.isEmpty()) {
                ResponseUtil.returnJsonMsg(response, 2, "参数错误", null);
                return;
            }

            Map params = new HashMap();
            params.put("pageSize", Integer.valueOf(pageSize));
            params.put("ltPublishTime", DateUtil.getDateString(ltPublishTimeLong));
            params.put("isPublished", Integer.valueOf(1));
            params.put("eqPicFlag", Integer.valueOf(hasPicture));
            params.put("inColumnIdList", columnIdList);
            params.put("orderByClause", "TOP_TIME DESC, PUBLISH_TIME DESC");
            PageList<Article> list = this.articleService.getList(params, 1, pageSize);

            for (Article article : list) {
                Map param = new HashMap();
                param.put("eqArticleId", article.getId());
                article.getArticleThumbPictureList().addAll(this.articleThumbPictureService.getList(param, 1, 10, false));
                if (article.getPicFlag().equals(Integer.valueOf(1))) {
                    param.put("orderByClause", "ORDER_NUM asc");
                    article.getArticlePictureList().addAll(this.articlePictureService.getList(param, 1, 1000, false));
                }
            }

            Object resultMap = new HashMap();
            if (!list.isEmpty()) {
                Article last = (Article)list.get(list.size() - 1);
                Long lastPublishTime = Long.valueOf(last.getPublishTime().getTime());
                ((Map)resultMap).put("ltPublishTime", lastPublishTime);
            }
            ((Map)resultMap).put("ret", Integer.valueOf(0));
            ((Map)resultMap).put("total", Integer.valueOf(list.getPageTurn() != null ? list.getPageTurn().getRowCount().intValue() : 0));
            ((Map)resultMap).put("list", list);

            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Object.class, new String[] { "ret", "msg", "list", "id", "channelId", "columnId", "title", "titleShort", "summary", "summaryFlag", "keyword", "keywordList", "sourceName", "firstPicUrl", "articleUrl", "publishTime", "publishStatus", "articleThumbPictureList", "picFlag", "clickNum", "total", "ltPublishTime", "articleWapUrl", "publishTimeStr", "picUrl" });

            String responseResult = JSONObject.toJSONString(resultMap, filter, new SerializerFeature[] { SerializerFeature.WriteDateUseDateFormat });
            ResponseUtil.returnJsonMsg(response, responseResult);
            return;
        }
        catch (Exception e) {
            ResponseUtil.returnJsonMsg(response, 1, "服务器处理错误", null);
        }
    }

    @RequestMapping({"/getListByBSColumns"})
    public void getListByBSColumns(HttpServletRequest request, HttpServletResponse response, ModelMap model)
    {
        try
        {
            HttpRequestInfo reqInfo = new HttpRequestInfo(request);
            Long ltPublishTimeLong = Long.valueOf(reqInfo.getLongParameter("ltPublishTime", -1L));
            int pageSize = reqInfo.getIntParameter("pageSize", 10);
            String columnIds = reqInfo.getParameter("columnIds", "");
            List columnIdList = StringUtil.str2IntList(columnIds);
            if (columnIdList.isEmpty()) {
                ResponseUtil.returnJsonMsg(response, 2, "参数错误", null);
                return;
            }

            Map params = new HashMap();
            params.put("pageSize", Integer.valueOf(pageSize));
            params.put("ltPublishTime", DateUtil.getDateString(ltPublishTimeLong));
            params.put("isPublished", Integer.valueOf(1));
            params.put("inColumnIdList", columnIdList);
            params.put("orderByClause", "TOP_TIME DESC, PUBLISH_TIME DESC");
            PageList<Article> list = this.articleService.getList(params, 1, pageSize);

            for (Article article : list) {
                Map param = new HashMap();
                param.put("eqArticleId", article.getId());
                article.getArticleThumbPictureList().addAll(this.articleThumbPictureService.getList(param, 1, 10, false));
                if (article.getPicFlag().equals(Integer.valueOf(1))) {
                    param.put("orderByClause", "ORDER_NUM asc");
                    article.getArticlePictureList().addAll(this.articlePictureService.getList(param, 1, 1000, false));
                }
            }

            Object resultMap = new HashMap();
            if (!list.isEmpty()) {
                Article last = (Article)list.get(list.size() - 1);
                Long lastPublishTime = Long.valueOf(last.getPublishTime().getTime());
                ((Map)resultMap).put("ltPublishTime", lastPublishTime);
            }
            ((Map)resultMap).put("ret", Integer.valueOf(0));
            ((Map)resultMap).put("total", Integer.valueOf(list.getPageTurn() != null ? list.getPageTurn().getRowCount().intValue() : 0));
            ((Map)resultMap).put("list", list);

            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Object.class, new String[] { "ret", "msg", "list", "id", "channelId", "columnId", "title", "titleShort", "summary", "summaryFlag", "keyword", "keywordList", "sourceName", "firstPicUrl", "articleUrl", "publishTime", "publishStatus", "articleThumbPictureList", "picFlag", "clickNum", "total", "ltPublishTime", "articleWapUrl", "publishTimeStr", "picUrl" });

            String responseResult = JSONObject.toJSONString(resultMap, filter, new SerializerFeature[] { SerializerFeature.WriteDateUseDateFormat });
            ResponseUtil.returnJsonMsg(response, responseResult);
            return;
        }
        catch (Exception e) {
            ResponseUtil.returnJsonMsg(response, 1, "服务器处理错误", null);
        }
    }
}